﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace Royaume
{
    public partial class newMembre : System.Web.UI.Page
    {
        Dictionary<SqlParameter, object> myParam = new Dictionary<SqlParameter, object>();
        cSql objSql = new cSql("(local)", "DB_USERS", "RoyaumesLibres", "123456");
        protected void Page_Load(object sender, EventArgs e)
        {
            this.ValidationSummary1.ForeColor = System.Drawing.Color.FromName("brown");
            if (!Page.IsPostBack)
            {
                
                DateTime today = DateTime.Now;
                int years = Convert.ToInt32(today.Year.ToString()) - 16;
                string sMonths = "Janvier;Février;Mars;Avril;Mai;Juin;Juillet;Aout;Septembre;Octobre;Novembre;Décembre";
                string[] aMonth = sMonths.Split(';');
                string sCountry = "Canada;United States;-------;Afghanistan;Åland Islands;Albania;Algeria;American Samoa;Andorra;Angola;Anguilla;Antarctica;Antigua And Barbuda;Argentina;Armenia;Aruba;Australia;Austria;Azerbaijan;Bahamas;Bahrain;Bangladesh;Barbados;Belarus;Belgium;Belize;Benin;Bermuda;Bhutan;Bolivia;Bosnia And Herzegovina;Botswana;Bouvet Island;Brazil;British Indian Ocean Territory;Brunei Darussalam;Bulgaria;Burkina Faso;Burundi;Cambodia;Cameroon;Cape Verde;Cayman Islands;Central African Republic;Chad;Chile;China;Christmas Island;Cocos (Keeling) Islands;Colombia;Comoros;Congo;Congo, The Democratic Republic Of The;Cook Islands;Costa Rica;Cote D'ivoire;Croatia;Cuba;Cyprus;Czech Republic;Denmark;Djibouti;Dominica;Dominican Republic;Ecuador;Egypt;El Salvador;Equatorial Guinea;Eritrea;Estonia;Ethiopia;Falkland Islands (Malvinas);Faroe Islands;Fiji;Finland;France;French Guiana;French Polynesia;French Southern Territories;Gabon;Gambia;Georgia;Germany;Ghana;Gibraltar;Greece;Greenland;Grenada;Guadeloupe;Guam;Guatemala;Guernsey;Guinea;Guinea-bissau;Guyana;Haiti;Heard Island And Mcdonald Islands;Holy See (Vatican City State);Honduras;Hong Kong;Hungary;Iceland;India;Indonesia;Iran, Islamic Republic Of;Iraq;Ireland;Isle Of Man;Israel;Italy;Jamaica;Japan;Jersey;Jordan;Kazakhstan;Kenya;Kiribati;Korea, Democratic People's Republic Of;Korea, Republic Of;Kuwait;Kyrgyzstan;Lao People's Democratic Republic;Latvia;Lebanon;Lesotho;Liberia;Libyan Arab Jamahiriya;Liechtenstein;Lithuania;Luxembourg;Macao;Macedonia, The Former Yugoslav Republic Of;Madagascar;Malawi;Malaysia;Maldives;Mali;Malta;Marshall Islands;Martinique;Mauritania;Mauritius;Mayotte;Mexico;Micronesia, Federated States Of;Moldova, Republic Of;Monaco;Mongolia;Montenegro;Montserrat;Morocco;Mozambique;Myanmar;Namibia;Nauru;Nepal;Netherlands;Netherlands Antilles;New Caledonia;New Zealand;Nicaragua;Niger;Nigeria;Niue;Norfolk Island;Northern Mariana Islands;Norway;Oman;Pakistan;Palau;Palestinian Territory, Occupied;Panama;Papua New Guinea;Paraguay;Peru;Philippines;Pitcairn;Poland;Portugal;Puerto Rico;Qatar;Reunion;Romania;Russian Federation;Rwanda;Saint Helena;Saint Kitts And Nevis;Saint Lucia;Saint Pierre And Miquelon;Saint Vincent And The Grenadines;Samoa;San Marino;Sao Tome And Principe;Saudi Arabia;Senegal;Serbia;Seychelles;Sierra Leone;Singapore;Slovakia;Slovenia;Solomon Islands;Somalia;South Africa;South Georgia And The South Sandwich Islands;Spain;Sri Lanka;Sudan;Suriname;Svalbard And Jan Mayen;Swaziland;Sweden;Switzerland;Syrian Arab Republic;Taiwan, Province Of China;Tajikistan;Tanzania, United Republic Of;Thailand;Timor-leste;Togo;Tokelau;Tonga;Trinidad And Tobago;Tunisia;Turkey;Turkmenistan;Turks And Caicos Islands;Tuvalu;Uganda;Ukraine;United Arab Emirates;United Kingdom;United States Minor Outlying Islands;Uruguay;Uzbekistan;Vanuatu;Venezuela;Viet Nam;Virgin Islands, British;Virgin Islands, U.S.;Wallis And Futuna;Western Sahara;Yemen;Zambia;Zimbabwe";
                this.radsexe.Items.Add("Féminin");
                this.radsexe.Items.Add("Masculin");
                this.radsexe.SelectedIndex = 0;
                this.radsexe.Items[0].Value = "F";
                this.radsexe.Items[1].Value = "M";
                string[] aCountry = sCountry.Split(';');
                foreach (string tempo in aCountry)
                {
                    this.cbocountry.Items.Add(tempo);
                }
                for (int i = years; i > years - 70; i--)
                {
                    this.cboyears.Items.Add(i.ToString());
                }
                int iIndex = 0;
                foreach (string mois in aMonth)
                {
                    this.cbomonths.Items.Add(mois);
                    this.cbomonths.Items[iIndex].Value = (iIndex + 1).ToString();
                    iIndex++;
                }
                for (int z = 1; z < 32; z++)
                {
                    this.cboday.Items.Add(z.ToString());
                }

            }
        }

        protected void btnsubmit_Click(object sender, EventArgs e)
        {
            if (this.cbocountry.SelectedIndex == 2)
            {
                this.showErrorCountry.Text = "* Choisisez un pays valide";
            }
            else
            {

                if (Page.IsValid)
                {
                    this.showErrorCountry.Text = "";
                    string sDate = this.cboday.SelectedValue.ToString() + "/" + this.cbomonths.SelectedValue.ToString() + "/" + this.cboyears.SelectedValue.ToString();
                    string sLogin = this.txtlogin.Text;
                    string sFirstName = this.txtfirstname.Text;
                    string sLastName = this.txtlastname.Text;
                    string sSexe = this.radsexe.SelectedValue.ToString();
                    string sPassword = cUtility.Encrypt(this.txtpassword.Text);
                    string sCity = this.txtcity.Text;
                    string sCountry = this.cbocountry.SelectedValue.ToString();
                    string sEmail = this.txtemail.Text;
                    int iAge = this.fnCalculAge(this.cboyears.SelectedValue.ToString() + "-" + this.cbomonths.SelectedValue.ToString() + "-" + this.cboday.SelectedValue.ToString());

                    myParam.Clear();
                    myParam.Add(new SqlParameter("@login", SqlDbType.VarChar, 50), sLogin);
                    myParam.Add(new SqlParameter("@password", SqlDbType.VarChar, 255), sPassword);
                    myParam.Add(new SqlParameter("@nom", SqlDbType.VarChar, 50), sFirstName);
                    myParam.Add(new SqlParameter("@prenom", SqlDbType.VarChar, 30), sLastName);
                    myParam.Add(new SqlParameter("@age", SqlDbType.Int), iAge);
                    myParam.Add(new SqlParameter("@sexe", SqlDbType.VarChar, 1), sSexe);
                    myParam.Add(new SqlParameter("@ville", SqlDbType.VarChar, 50), sCity);
                    myParam.Add(new SqlParameter("@email", SqlDbType.VarChar, 50), sEmail);
                    myParam.Add(new SqlParameter("@pays", SqlDbType.VarChar, 25), sCountry);
                    myParam.Add(new SqlParameter("@dateAnniversaire", SqlDbType.DateTime),"1996-01-01");
                    objSql.fnStoredProc("spInscriptionUser", myParam);
                    myParam.Clear();
                    myParam.Add(new SqlParameter("@login", SqlDbType.VarChar, 16), sLogin);
                    string sMembreInfo = objSql.fnExecSalarString("spGetPkMembre", myParam);
                    string[] info = sMembreInfo.Split(';');
                    Session.Add("pkUser", info[0]);
                    Session.Add("nomUser", info[1]);
                    Session.Add("toredirect", true);
                    Session.Add("isLog", true);
                    Response.Redirect("WebForm1.aspx");
                    
                }
            }
        }

        protected int fnCalculAge(string sDate)
        {
            DateTime bday = Convert.ToDateTime(sDate);
            DateTime now = DateTime.Today;
            int age = now.Year - bday.Year;
            if (bday > now.AddYears(-age))
            {
                age--;
            }
            return age;
        }
    }
}